home *** CD-ROM | disk | FTP | other *** search
- Path: news1.erols.com!newsmaster@erols.com
- From: Chris Cobb <ccobb@erols.com>
- Newsgroups: comp.lang.c++
- Subject: Re: How to get at base class fields from member functions?
- Date: 17 Mar 1996 17:20:15 GMT
- Organization: CSEG, Inc.
- Message-ID: <4ihhkf$2ni@news5.erols.com>
- References: <internews46B00D1FBD@argonet.co.uk>
- NNTP-Posting-Host: ccobb.erols.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22KIT (Windows; U; 16bit)
-
- Dave Mullard <dmullard@argonet.co.uk> wrote:
- >Given three classes A, B and C I want to have multiple As for each B and
- >multiple Bs for each C. To do this I could create the following.
- >
- >class b1 : public B
- >{
- >A a1;
- >A a2;
- >A a3;
- >};
- >
- >class b2 : public B
- >{
- >A a1;
- >A a2;
- >A a3;
- >A a4;
- >A a5;
- >};
- >
- >class c1 : public C
- >{
- >b1 b1;
- >b2 b2;
- >};
- >
- >The question is, how within functions for class B do I access fields
- >within class C, and similarly, how within the class A functions do I
- >access fields within class B?
- >
- >Any help would be appreciated.
- >
-
- You have encountered the wall of encapsulation. It is considered poor
- form to violate this wall without good reason. The need to violate this
- wall may indicate poor design. However, C++ is not so rigid as to
- disallow this. The way this is done is to have the class whose members
- need to be accessed declare the class who wants to access them a friend:
-
- class C
- {
- friend class B;
- // ...
- };
-
- class B
- {
- friend class A;
- // ...
- };
-
- Chris
- ccobb@cseg.com
-
-